﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Bll
{
    class CPicsManage
    {
        /// <summary>
        /// 纯粹是创建一个新的对象
        /// </summary>
        /// <returns></returns>
        public static CPics CreatePics()
        {
            return new CPics();
        }
		
		public static List<CPics> LoadAllPics()
		{
			List<CPics> lpics = new List<CPics>();
            SqlDataReader reader = BLL.SqlHelper.ExecuteReader(CConnection.getConnString(), "sp_GetPicByCondition");
            while (reader.Read())
            {
                CPics c = new CPics();
                c.imgID = reader["imgID"].ToString();
                c.imgURL = reader["imgURL"].ToString();

                lpics.Add(c);
            }
			return lpics;
		}
		
        /// <summary>
        /// 将对象保存到数据库中去
        /// </summary>
        /// <param name="c">用户对象</param>
        /// <returns>成功返回保存的对象，否则为空</returns>
        public static CPics Save(CPics c)
        {
            try
            {
                int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_AddPic", c.ID, c.imgID, c.imgURL);

                if (0 == num)
                    return null;
                return c;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        /// <summary>
        /// 将修改后对象保存到数据库中去
        /// </summary>
        /// <param name="c">是否成功</param>
        /// <returns>成功返回真，否则假</returns>
        public static bool Update(CPics c)
        {
            try
            {
                int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_SavePic", c.ID, c.imgID, c.imgURL);
                if (0 == num)
                    return false;
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="c">是否成功</param>
        /// <returns>成功返回真，否则假</returns>
        public static bool Delete(CPics c)
        {
            try
            {
                int num = BLL.SqlHelper.ExecuteNonQuery(CConnection.getConnString(), "sp_RemovePic", c.ID);
                if (0 == num)
                    return false;
                return true;
            }
            catch (Exception e)
            {
                return false;
            }


        }


        /// <summary>
        /// 通过id查找
        /// </summary>
        /// <param name="ID">用户id</param>
        /// <returns>用户对象</returns>
        public static CPics GetBasicUser(String ID)
        {
            SqlDataReader reader = BLL.SqlHelper.ExecuteReader(CConnection.getConnString(), "sp_GetPic", ID);
            try
            {
                
                if (reader.Read())
                {
                    CPics c = new CPics();
                    c.imgID = reader["imgID"].ToString();
                    c.imgURL = reader["imgURL"].ToString();

                  
                    return c;
                }
                return null;
            }
            catch (Exception e)
            {
                
                return null;
            }
            finally
            {
                reader.Close();
            }

        }

        
       
        /// <summary>
        /// 通过condition的条件找对象，condition 可以这样写and (a=1 or b=4)
        /// </summary>
        /// <param name="condition">条件</param>
        /// <returns>一个集合</returns>
        public List<CPics> GetByCondition(String condition)
        {

            List<CPics> users = new List<CPics>();
            SqlDataReader reader = BLL.SqlHelper.ExecuteReader(CConnection.getConnString(), "select id,imgID,imgURL from UserBasic where 1=1 " + condition);

            try
            {
                
                while (reader.Read())
                {
                    CPics c = new CPics();
                    c.imgID = reader["imgID"].ToString();
                    c.imgURL = reader["imgURL"].ToString();

                    users.Add(c);
                }

                return users;
            }
            catch (Exception e)
            {

                return null;
            }
            finally
            {
                reader.Close();
            }
        }
    }
}
